package ${packageName}.controller;

import ${packageName}.domain.${entityName};
import ${packageName}.utils.response.BaseResponse;
import ${packageName}.service.${serviceName};
import ${packageName}.utils.common.PageEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.paginate.Page;
import java.util.List;
import java.util.Arrays;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * ${remark}控制层
 * @Author: 不是菜鸡爱编程
 * @Date:
 * @Description：
 */
@RestController
@Api(tags = "${remark}接口")
@RequestMapping("${modelName}")
public class ${controllerName}{

    @Autowired
    private ${serviceName} ${serviceName?uncap_first};

    /**
     * 保存
     *
     * @param ${entityName?uncap_first} ${remark}
     * @return BaseResponse
     */
    @PostMapping("/save")
    @ApiOperation("保存${remark}")
    public BaseResponse<?> save(@RequestBody @ApiParam("${remark}") ${entityName} ${entityName?uncap_first}){
        boolean saveFlag = ${serviceName?uncap_first}.save(${entityName?uncap_first});
        if(saveFlag){
            return BaseResponse.success("保存成功");
        }else{
            return BaseResponse.error("保存失败");
        }
    }

    /**
     * 根据主键查询
     *
     * @param id
     * @return BaseResponse
     */
    @GetMapping("/info/{id}")
    @ApiOperation("根据主键查询${remark}")
    public BaseResponse<?> info(@PathVariable @ApiParam("${remark}主键") Integer id){
        return BaseResponse.success(${serviceName?uncap_first}.getById(id),"查询成功");
    }

    /**
     * 列表查询
     *
     * @return BaseResponse
     */
    @PostMapping("/list")
    @ApiOperation("列表查询${remark}")
    public BaseResponse<?> list(@RequestBody @ApiParam("${remark}分页参数") PageEntity<${entityName}> ${entityName?uncap_first}Page){
        ${entityName} ${entityName?uncap_first} = ${entityName?uncap_first}Page.getData();
        QueryWrapper queryWrapper = getQueryWrapper(${entityName?uncap_first});
        return BaseResponse.success(${serviceName?uncap_first}.page(new Page<>(${entityName?uncap_first}Page.getPageNum(),${entityName?uncap_first}Page.getPageSize()),queryWrapper),"查询成功");
    }

    /**
     * 删除
     *
     * @param ids
     * @return BaseResponse
     */
    @DeleteMapping("/delete/{ids}")
    @ApiOperation("删除${remark}")
    public BaseResponse<?> delete(@PathVariable @ApiParam("${remark}主键(列表)") Integer[] ids){
        return BaseResponse.success(${serviceName?uncap_first}.removeByIds(Arrays.asList(ids)),"删除成功");
    }


    /**
     * 修改
     *
     * @param ${entityName?uncap_first} ${remark}
     * @return BaseResponse
     */
    @PutMapping("/edit")
    @ApiOperation("修改${remark}")
    public BaseResponse<?> edit(@RequestBody @ApiParam("${remark}") ${entityName} ${entityName?uncap_first}){
        return BaseResponse.success(${serviceName?uncap_first}.updateById(${entityName?uncap_first}),"修改成功");
    }

    /**
     * 获取queryWrapper
     *
     * @param ${entityName?uncap_first} ${remark}
     * @return {@link QueryWrapper}
     */
    private QueryWrapper getQueryWrapper(${entityName} ${entityName?uncap_first}){
        return QueryWrapper.create()
                <#list columns as column>
                    .eq(${entityName}::get${column.propertyName},${entityName?uncap_first}.get${column.propertyName}(),${entityName?uncap_first}.get${column.propertyName}()!=null)<#if !column_has_next>;</#if>
                </#list>
    }
}